

package edu.upn.sigemfi.plt.bc;

//import com.sun.org.apache.bcel.internal.generic.Select;
import edu.upn.sigemfi.ope.bc.MonedaFacadeLocal;
import edu.upn.sigemfi.ope.be.Moneda;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class MonedaDelegate {



    private MonedaFacadeLocal local;

    public MonedaDelegate() {
        local = lookupMonedaFacade();
    }

    public void crear(Moneda Moneda) throws Exception {
        try {
            local.create(Moneda);
        } catch (Exception e) {
            local = lookupMonedaFacade();
            throw new Exception("Crear Moneda. " + e.getMessage(), e);
        }
    }

    public void eliminar(Moneda Moneda) throws Exception {
        try {
            local.remove(Moneda);
        } catch (Exception e) {
            local = lookupMonedaFacade();
            throw new Exception("Eliminar Moneda. " + e.getMessage(), e);
        }
    }

    public void editar(Moneda Moneda) throws Exception {
        try {
            local.edit(Moneda);
        } catch (Exception e) {
            local = lookupMonedaFacade();
            throw new Exception("Editar Moneda. " + e.getMessage(), e);
        }
    }

    public Moneda buscar(long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupMonedaFacade();
            throw new Exception("Buscar Moneda. " + e.getMessage(), e);
        }
    }

    public List<Moneda> ListarMonedaes() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupMonedaFacade();
            throw new Exception("Listar Moneda. " + e.getMessage(), e);
        }
    }

    public List<Moneda> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupMonedaFacade();
            throw new Exception("Listar Moneda. " + e.getMessage(), e);
        }
    }

    private MonedaFacadeLocal lookupMonedaFacade() {
        try {
            Context c = new InitialContext();
            return (MonedaFacadeLocal) c.lookup("java:comp/env/MonedaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }




}
